package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ContractInfo;
import ywpt.model.ServiceType;
import ywpt.service.config.ServiceTypeService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ServiceTypeAction extends BaseAction implements
ModelDriven<ServiceType>, Preparable{

	/**
	 * 服务类型Action
	 */
	private static final long serialVersionUID = -3499007494252696586L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	
	private ServiceType serviceTypeInfo;
	private List<ServiceType> serviceTypeList = new ArrayList<ServiceType>();
	private ServiceTypeService  serviceTypeService;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	public ServiceType getServiceTypeInfo() {
		return serviceTypeInfo;
	}

	public void setServiceTypeInfo(ServiceType serviceTypeInfo) {
		this.serviceTypeInfo = serviceTypeInfo;
	}

	public List<ServiceType> getServiceTypeList() {
		return serviceTypeList;
	}

	public void setServiceTypeList(List<ServiceType> serviceTypeList) {
		this.serviceTypeList = serviceTypeList;
	}

	public ServiceTypeService getServiceTypeService() {
		return serviceTypeService;
	}

	public void setServiceTypeService(ServiceTypeService serviceTypeService) {
		this.serviceTypeService = serviceTypeService;
	}
//**********************************************************************
	@Override
	public ServiceType getModel() {
		return this.serviceTypeInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.serviceTypeInfo = serviceTypeService.getServiceTypeInfoById(id);
		}else{
			this.serviceTypeInfo = new ServiceType();
		}
	}
//********************************************************************
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(ServiceType.class);
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = serviceTypeService.getServiceTypeInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("typeCode"));
		query.setProjection(null);
		serviceTypeList.addAll(serviceTypeService.getServiceTypeInfos(query, pageSize,
				rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{
		String typeCode =this.serviceTypeService.createTypeCode();
		serviceTypeInfo.setTypeCode(typeCode);
		return INPUT;
	}
	
	public String edit() 
	{
		this.serviceTypeInfo=this.serviceTypeService.getServiceTypeInfoById(id);
		return INPUT;
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		try 
		{
			if (id == null) 
			{
				serviceTypeService.saveServiceTypeInfo(serviceTypeInfo);
				this.addActionMessage("新增服务类型信息已保存!");
			} 
			else 
			{
				serviceTypeService.updateServiceTypeInfo(serviceTypeInfo);
				this.addActionMessage("服务类型信息更改完成!");
			}
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
				this.addActionError("保存错误:" + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
			DetachedCriteria contractQuery = DetachedCriteria.forClass(ContractInfo.class);
			contractQuery.add(Restrictions.eq("type", serviceTypeService.getServiceTypeInfoById(id)));
			int	totalRows = serviceTypeService.getServiceTypeInfoCountByCriteria(contractQuery);
			if(totalRows>0)
			{
				this.addActionMessage("该客户已经发生过业务逻辑，不能删除！");
				return INPUT;
			}else{
				serviceTypeService.removeServiceTypeInfo(id);
				return SUCCESS;				
			}

		}
		this.addActionMessage("类型编号为空，请选择具体的记录！");
		return INPUT;
	}
	
	public String select() 
	{
		serviceTypeList.addAll(serviceTypeService.getServiceTypeInfos());
		return SUCCESS;
	}

}
